package com.team.app.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.team.app.enums.Flag;
import com.team.app.enums.ObjectType;
import com.team.app.enums.UrlFixed;
import com.team.app.model.Active;
import com.team.app.model.Auth;
import com.team.app.model.AuthAcept;
import com.team.app.service.AuthAceptService;
import com.team.app.util.ConvertUtil;

public class AbstractAuthCheck {
	@Autowired	
	private AuthAceptService authAceptService;
	
	
	public boolean checkView(HttpSession s,String pathBase){
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		UrlFixed url = UrlFixed.getByUrl(pathBase);
		String name = url.name() != null ? url.name().toLowerCase() : "";		
		Active active = new Active(name);	
		
		if(loginAuth.getObjectType().equals(ObjectType.TYPE_SERVICE.getKey())){		
			active.setAdd(Flag.ON.getKey());
			active.setView(Flag.ON.getKey());
			active.setEdit(Flag.ON.getKey());
			active.setDelete(Flag.ON.getKey());
			s.setAttribute(name,active);
			return true;
		}else{			
			Long urlId = Long.parseLong(url.getKey());
			AuthAcept acept = authAceptService.find(urlId, loginAuth.getRoleId());
			if(!ConvertUtil.isNull(acept)){
				active.setAdd(acept.getActiveInsert());
				active.setView(acept.getActiveView());
				active.setEdit(acept.getActiveUpdate());
				active.setDelete(acept.getActiveDelete());				
				s.setAttribute(name,active);
				return (acept.getActiveView() != 0 ? true : false);
			}
		}
		return false;
	}
	
	public boolean checkAdd(HttpSession s,String pathBase){
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		if(loginAuth.getObjectType().equals(ObjectType.TYPE_SERVICE.getKey())){
			return true;
		}else{
			UrlFixed url = UrlFixed.getByUrl(pathBase);
			Long urlId = Long.parseLong(url.getKey());
			AuthAcept acept = authAceptService.find(urlId, loginAuth.getRoleId());
			if(!ConvertUtil.isNull(acept)){
				return (acept.getActiveInsert() != 0 ? true : false);
			}
		}
		return false;
	}
	public boolean checkEdit(HttpSession s,String pathBase){
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		if(loginAuth.getObjectType().equals(ObjectType.TYPE_SERVICE.getKey())){
			return true;
		}else{
			UrlFixed url = UrlFixed.getByUrl(pathBase);
			Long urlId = Long.parseLong(url.getKey());
			AuthAcept acept = authAceptService.find(urlId, loginAuth.getRoleId());
			if(!ConvertUtil.isNull(acept)){
				return (acept.getActiveUpdate() != 0 ? true : false);
			}
		}
		return false;
	}
	public boolean checkDelete(HttpSession s,String pathBase){
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		if(loginAuth.getObjectType().equals(ObjectType.TYPE_SERVICE.getKey())){
			return true;
		}else{
			UrlFixed url = UrlFixed.getByUrl(pathBase);
			Long urlId = Long.parseLong(url.getKey());
			AuthAcept acept = authAceptService.find(urlId, loginAuth.getRoleId());
			if(!ConvertUtil.isNull(acept)){
				return (acept.getActiveDelete() != 0 ? true : false);
			}
		}
		return false;
	}
	
	public String accessDenied() {
		return "/auth/access-denied";
	}
	
	
}
